package com.luyuan.crm.member.service.impl;

import java.util.Date;

import com.luyuan.crm.member.model.Customer;
import com.luyuan.crm.member.model.MemberIntegral;
import com.luyuan.crm.member.model.MemberIntegralRecord;
import com.luyuan.crm.member.service.MemberIntegralRecordService;
import com.luyuan.crm.member.service.MemberIntegralService;
import com.ruijie.foundation.dao.support.HibernateEntityDao;

public class MemberIntegralServiceImpl extends HibernateEntityDao<MemberIntegral> implements MemberIntegralService {
    private MemberIntegralRecordService memberIntegralRecordService;

    @Override
    public MemberIntegral getMemberIntegral(Customer customer) {
        MemberIntegral memberIntegral = safeGet(customer.getId());
        if (memberIntegral == null) {
            memberIntegral = new MemberIntegral();
        }
        return memberIntegral;
    }

    @Override
    public void addMemberIntegral(MemberIntegral memberIntegral, int integral, String remarks) {
        save(memberIntegral);

        // 积分记录
        MemberIntegralRecord usedRecord = new MemberIntegralRecord();
        usedRecord.setMember(memberIntegral.getMember());
        usedRecord.setUsedTime(new Date());
        usedRecord.setIntegral(integral);
        usedRecord.setTotalIntegral(memberIntegral.getTotalIntegral());
        usedRecord.setUsedDetail(remarks);
        memberIntegralRecordService.addMemberIntegralRecord(usedRecord);
    }

    @Override
    public void updateMemberCurrentIntegral(MemberIntegral memberIntegral, int integral, String remarks) {
        memberIntegral.setCurrentIntegral(memberIntegral.getCurrentIntegral() + integral);
        update(memberIntegral);

        // 积分记录
        MemberIntegralRecord usedRecord = new MemberIntegralRecord();
        usedRecord.setMember(memberIntegral.getMember());
        usedRecord.setUsedTime(new Date());
        usedRecord.setIntegral(integral);
        usedRecord.setTotalIntegral(memberIntegral.getTotalIntegral());
        usedRecord.setUsedDetail(remarks);
        memberIntegralRecordService.addMemberIntegralRecord(usedRecord);
    }

    public void setMemberIntegralRecordService(MemberIntegralRecordService memberIntegralRecordService) {
        this.memberIntegralRecordService = memberIntegralRecordService;
    }
}
